package handler

import (

	"golang.org/x/net/context"
	client "git.oschina.net/fanbuchi/xgggh/oauth2/proto/client"
	"git.oschina.net/fanbuchi/xgggh/oauth2/model"
	"git.oschina.net/fanbuchi/xgggh/oauth2/db"
	"encoding/json"
	"git.oschina.net/fanbuchi/xgggh/oauth2/service"
	"github.com/satori/go.uuid"
)

type Client struct{}



//micro query com.xgggh.srv.oauth2 ClientSrv.Create '{"redirectUri":"www.baidu.com"}'
//micro query com.xgggh.srv.oauth2 ClientSrv.Create '{"redirectUri":"www.sina.com"}'
//micro query com.xgggh.srv.oauth2 ClientSrv.Create '{"redirectUri":"www.hcxdi.com"}'

// Call is a single request handler called via client.Call or the generated client code
func (e *Client) Create(ctx context.Context, req *client.ClientCreateReq, rsp *client.ClientCreateRsp) error {
	v4,_:=uuid.NewV4()
	key:=v4.String()
	v4Secret,_:=uuid.NewV4()
	secret:=v4Secret.String()

	_client:=model.Client{RedirectUri:req.RedirectUri,
		Key:key,
		Secret:secret}
	err:=db.GetDB().Create(&_client).Error
	return err
}
//micro query com.xgggh.srv.oauth2 ClientSrv.Query '{"id":245488686689419265}'

func (e *Client) Query(ctx context.Context, req *client.ClientQueryReq, rsp *client.ClientQueryRsp) error {
	var _client model.Client;
	err:=db.GetDB().First(&_client, req.Id).Error
	if _client.ID<=0 {
		return nil
	}

	var deleted int64
	if _client.DeletedAt!=nil {
		deleted=_client.DeletedAt.Unix()
	}

	rsp.Data=&client.ClientMsg{Id:int64(_client.ID),
		RedirectUri:_client.RedirectUri,
		Key:_client.Key,
		Secret:_client.Secret,
		CreatedAt:_client.CreatedAt.Unix(),
		UpdatedAt:_client.UpdatedAt.Unix(),
		DeletedAt:deleted,
		}
	return err
}

//micro query com.xgggh.srv.oauth2 ClientSrv.Update '{"id":245488686689419265,"client":{"redirectUri":"http://callback.baidu.com/api","secret":"secret_update"}}'

func (e *Client) Update(ctx context.Context, req *client.ClientUpdateReq, rsp *client.ClientUpdateRsp) error {
	_client:=req.Client
	if  _client==nil{
		return nil
	}
	_client.Id=req.Id
	err:= db.GetDB().Model(&_client).Updates(model.Client{
		RedirectUri:_client.RedirectUri,
		Secret:_client.Secret,
	}).Error
	if err ==nil{
		rsp.Data=_client
	}
	return err
}
//micro query com.xgggh.srv.oauth2 ClientSrv.Delete '{"id":245488686689419265}'

func (e *Client) Delete(ctx context.Context, req *client.ClientDeleteReq, rsp *client.ClientDeleteRsp) error {
	_client:=model.Client{}
	_client.ID=uint(req.Id)
	err:=db.GetDB().Delete(&_client).Error
	return err
}

//micro query com.xgggh.srv.oauth2 ClientSrv.Search '{"page":1,"per":10}'
//search by userName,metaUserId ...
//micro query com.xgggh.srv.oauth2 ClientSrv.Search '{"page":1,"per":10,"params":{"key":"15108280907"}}'
func (e *Client) Search(ctx context.Context, req *client.ClientSearchReq, rsp *client.ClientSearchRsp) error {
	results,err:=service.ClientSearch(req)
	if err!=nil {
		return err
	}
	var result [] *client.ClientMsg;
	data,err:=json.Marshal(results)
	json.Unmarshal(data,&result)
	if err ==nil{
		rsp.Data=result
	}
	return err
}